package message;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class temperatureServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		/*暴躁的 choleric；乐观的：sanguine；冷漠的：phlegmatic；忧郁的：melancholic；*/
		String temperamentName,dz,dx,ny,yy,temp;
		int choleric=0,sanguine=0,phlegmatic=0,melancholic=0,scor;
		String[] temperament={"choleric","sanguine","phlegmatic","melancholic"};
		String[] dzArray = {"2","6", "9","14","17","21","27","31","36","38","42","48","50","54","58"};
		String[] dxArray = {"4","8","11","16","19","23","25","29","34","40","44","46","52","56","60"};
		String[] nnArray = {"1","7","10","13","18","22","26","30","33","39","43","45","49","55","57"};
		String[] yyArray = {"3","5","12","15","20","24","28","32","35","37","41","47","51","53","59"};
		String id=new String(request.getParameter("student_id").getBytes("ISO-8859-1"),"UTF-8");
		String password=new String(request.getParameter("student_password").getBytes("ISO-8859-1"),"UTF-8");
		String name=new String(request.getParameter("student_name").getBytes("ISO-8859-1"),"UTF-8");
		String className=new String(request.getParameter("student_className").getBytes("ISO-8859-1"),"UTF-8");
		/*循环加出各个气质类型的分数*/
		for(int i=0;i<dzArray.length;i++){
			dz=new String(request.getParameter(dzArray[i]));
			choleric+=Integer.parseInt(dz);
			dx=new String(request.getParameter(dxArray[i]));
			sanguine+=Integer.parseInt(dx);
			ny=new String(request.getParameter(nnArray[i]));
			phlegmatic+=Integer.parseInt(ny);
			yy=new String(request.getParameter(yyArray[i]));
			melancholic+=Integer.parseInt(yy);
		}
		/*/循环加出各个气质类型的分数*/
		/*按分值大小进行排序*/
		int[] score={choleric,sanguine,phlegmatic,melancholic};
	    for (int i=0;i<score.length-1;i++) {
	        for (int j=i+1;j<score.length;j++) {
	            if (score[i]<score[j]) {
	            	scor=score[j];
	            	score[j]=score[i];
	                score[i]=scor;                
	            	temp=temperament[j];
	            	temperament[j]=temperament[i];
	            	temperament[i]=temp;
	            }
	        }
	    }
	    /*/按分值大小进行排序*/
	    /*比较分值给出测试结果*/
	    if(score[0]-score[1]>=4){temperamentName=temperament[0];}
	    else if(score[1]-score[2]>=4){temperamentName=temperament[0]+"-"+temperament[1];}
	    else if(score[2]-score[3]>=4){temperamentName=temperament[0]+"-"+temperament[1]+"-"+temperament[2];}
	    else{temperamentName=temperament[0]+"-"+temperament[1]+"-"+temperament[2]+"-"+temperament[3];}

    	System.out.println("分值："+temperament[0]+"-"+score[0]+"、"+temperament[1]+"-"+score[1]+"、"+temperament[2]+"-"+score[2]+"、"+temperament[3]+"-"+score[3]);

	    /*/比较分值给出测试结果*/
		try{
			Connection con=null;
			PreparedStatement stmt=null;
			Class.forName("com.mysql.jdbc.Driver");
			String url="jdbc:mysql://localhost:3306/bedroomallocation?useUnicode=true&characterEncoding=gbk";
			con=DriverManager.getConnection(url,"root","root");
			String sql="update student set temperament=?"+",message=?"+" where id='"+id+"'";
			stmt=con.prepareStatement(sql);
			stmt.setString(1, temperamentName);
			stmt.setString(2, "100");
			stmt.execute();
			stmt.close();
			HttpSession session=request.getSession();
			ArrayList hobby=new ArrayList();
			LoginBean bean=new LoginBean();
			bean.setAccount(id);
			bean.setPassword(password);
			bean.setName(name);
			bean.setClassName(className);
			bean.setMessage("100");
			hobby.add(bean);
			if(request.getSession().getAttribute("login")!=null){
				request.getSession().removeAttribute("login");
				request.getSession().setAttribute("login",hobby);
			}
			session.setAttribute("login", hobby);
			response.sendRedirect("http://localhost:8888/BedroomAllocation/index.jsp");
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		doGet(request,response);
	}
	
	public void init() throws ServletException {
		// Put your code here
	}
	
}
